
capture log close
log using $log/AED_analysis_02_main_effects, text replace

/* ------------------------------------------------------------------------------------ */
/* --- Estimation results Table 2  ---------------------------------------------------- */
/* ------------------------------------------------------------------------------------ */ 


clear
clear matrix
clear mata
set maxvar 10000
set matsize 2000
set more off

set trace off
   
#d;
global xvars " vac_rate aloq   
				share_agriculture_avg share_industry_avg share_service_avg wanderungen_avg bip_avg 
				iza_alter_kat1 iza_alter_kat2 iza_alter_kat3 iza_alter_kat4 iza_sex iza_famstd
				i.iza_kind i.iza_abschluss_kat i.iza_ausb_kat
				iza_last_daily_income i.iza_beruf_kat
				iza_ende_last iza_vztz_last iza_zeit_la
				iza_sv_tminus1 iza_sv_tminus2 iza_sv_tminus3
				iza_lnwage_tminus1 iza_lnwage_tminus2 iza_lnwage_tminus3 
				i.year i.entry_m";
				#d cr
	
		*Merge variable on approximated regional MP expenditures
		use $orig/mobihilfen_all.dta, clear


		gen ausgaben_uebergang = (uebergang*241)
		gen ausgaben_ausruestung = (ausruestung*128)
		gen ausgaben_fahrkosten = (fahrkosten*761) 
		gen ausgaben_trennung = (trennung*881)
		gen ausgaben_umzug = (umzug*1315)
		gen ausgaben_reisekosten = (reisekosten*101) 



		gen ausgaben_mp = (ausgaben_fahrkosten + ausgaben_trennung + ausgaben_umzug + ausgaben_reisekosten)/1000000

		replace year = year + 1

		keep ba year ausgaben_mp 
		rename ba region
		tempfile ausgaben
		save `ausgaben'
	
		use $data/data_est_border, clear
	
		keep if eintritt<=200805 & eintritt>=200706
	
		joinby region year using `ausgaben',unmatched(master)
		tab _merge
		drop _merge
		label var ausgaben_mp "MP Expenditures in 1,000,000€"
	
	
		#d;
		egen month_sv1_24 = rsum(sv_tplus_1 sv_tplus_2 sv_tplus_3 sv_tplus_4 sv_tplus_5 sv_tplus_6 sv_tplus_7 sv_tplus_8 sv_tplus_9 sv_tplus_10 
		sv_tplus_11 sv_tplus_12 sv_tplus_13 sv_tplus_14 sv_tplus_15 sv_tplus_16 sv_tplus_17 sv_tplus_18 sv_tplus_19 sv_tplus_20 sv_tplus_21 sv_tplus_22 sv_tplus_23 sv_tplus_24)
		;
	
		egen earnings1_24 = rsum(daily_income_tplus_1 daily_income_tplus_2 daily_income_tplus_3 daily_income_tplus_4 daily_income_tplus_5
		daily_income_tplus_6 daily_income_tplus_7 daily_income_tplus_8 daily_income_tplus_9 daily_income_tplus_10 daily_income_tplus_11
		daily_income_tplus_12 daily_income_tplus_13 daily_income_tplus_14 daily_income_tplus_15 daily_income_tplus_16 
		daily_income_tplus_17 daily_income_tplus_18 daily_income_tplus_19 daily_income_tplus_20
		daily_income_tplus_21 daily_income_tplus_22 daily_income_tplus_23 daily_income_tplus_24);
		replace earnings1_24 = earnings1_24*30;
		#d cr
	
	

	
	
	
	global outc " month_sv1_24 earnings1_24 fahrkosten_24 trennungskosten_24 umzugskosten_24 reisekosten_24"
	

	bys iza_id: gen insample = 1 if _n==1
	
	foreach var in $outc {
		qui reg `var' log_intensity_alt $xvars pair_* [pw=1/fweight_reg], cluster(ba)
		matrix b=e(b)'
		
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		qui sum `var' if e(sample)==1 & insample==1
		matrix m = r(mean)
		scalar r = 100*(b[1,1]/m)
		
		count if e(sample)==1 & insample==1
		matrix n=e(N)
		matrix d1_`var'=b[1..1,1..1],se,r,n
		
		qui ivregress 2sls `var' $xvars pair_* (ausgaben_mp = log_intensity_alt) [pw=1/fweight_reg], cluster(ba)
	    matrix b=e(b)'
		matrix n=e(N)
		matrix v==e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		sum `var' if e(sample)==1 & insample==1
		scalar m=r(mean)
		scalar r = 100*(b[1,1]/m)
		matrix d2_`var'=b[1..1,1..1],se,r,n
		
		matrix d_`var' = d1_`var',d2_`var'
	}
	
	



matrix d = d_umzugskosten_24\d_fahrkosten_24\d_trennungskosten_24\d_reisekosten_24\d_month_sv1_24\d_earnings1_24


matrix rownames d = "Relocation assistance" "Commuting assistance" "Separation assistance" "Travel cost assistance" "Months employed" "Earnings"

matrix colnames d = "Reduced form" "SE" "%-effect"  "2SLS" "SE" "%-effect"
mat list d


log close
